package com.hoperun.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hoperun.bean.AppFeedbacks;
import com.hoperun.dao.AppFeedbacksDAO;
import com.hoperun.framework.ibatis.page.BootGrid;
import com.hoperun.framework.ibatis.page.BootGridPage;
import com.hoperun.framework.query.Criteria;
import com.hoperun.service.IAppFeedbacksSV;
import com.hoperun.utils.Constant;

@Service("appFeedbacksSV")
public class AppFeedbacksSVImpl implements IAppFeedbacksSV {
	@Resource
	private AppFeedbacksDAO appFeedbacksDAO;

	@Override
	public void delete(long id) throws Exception {
		appFeedbacksDAO.delete(id);
	}

	@Override
	public AppFeedbacks queryById(long id) throws Exception {
		return appFeedbacksDAO.queryById(id);
	}

	@Override
	public AppFeedbacks save(AppFeedbacks appFeedbacks) throws Exception {
		appFeedbacksDAO.insert(appFeedbacks);
		return appFeedbacks;
	}

	@Override
	public void update(AppFeedbacks appFeedbacks) throws Exception {
		appFeedbacksDAO.update(appFeedbacks);
	}

	@Override
	public void saveOrUpdate(AppFeedbacks appFeedbacks) throws Exception {
		if (appFeedbacks == null)
			return;
		if (null != appFeedbacks.getFdId() && appFeedbacks.getFdId() > 0) {
			update(appFeedbacks);
		} else {
			save(appFeedbacks);
		}
	}

	@Override
	public void saveOrUpdate(List<AppFeedbacks> list) throws Exception {
		if (list != null && list.size() > 0) {
			for (AppFeedbacks appFeedbacks : list) {
				saveOrUpdate(appFeedbacks);
			}
		}
	}

	@Override
	public List<AppFeedbacks> queryByCondition(Criteria criteria)
			throws Exception {
		return appFeedbacksDAO.queryByCondition(criteria);
	}

	@Override
	public int queryCountByCondition(Criteria criteria) throws Exception {
		return appFeedbacksDAO.queryCountByCondition(criteria);
	}

	@Override
	public BootGridPage<AppFeedbacks> queryPage(Criteria criteria,
			BootGrid<AppFeedbacks> grid) throws Exception {

//		AppFeedbacks model = grid.getEntity();

		int totalCount = appFeedbacksDAO.queryCountByCondition(criteria);

		if (0 < totalCount) {

			Map<String, String> sorts = grid.getSort();

			/** 设置排序规则 **/
			if (null != sorts && sorts.size() > Constant.ZERO) {
				Set<String> keySets = sorts.keySet();
				String sort = "";
				for (String key : keySets) {
					sort += " , " + key + " " + sorts.get(key);
				}
				criteria.orderBy(sort.replaceFirst(",", ""));
			}
			if (grid.getCurrent() >= Constant.ZERO) {
				int start = (grid.getCurrent() - Constant.ONE)
						* grid.getRowCount();
				int size = grid.getRowCount();

				criteria.limit(start, size);
			}
			List<AppFeedbacks> list = appFeedbacksDAO
					.queryByCondition(criteria);
			BootGridPage<AppFeedbacks> page = new BootGridPage<AppFeedbacks>(
					grid.getCurrent(), grid.getRowCount(), totalCount, list);

			return page;
		}
		return new BootGridPage<AppFeedbacks>(grid.getCurrent(),
				grid.getRowCount(), totalCount, new ArrayList<AppFeedbacks>());

	}
}
